﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using thel.code;
using thel.math;
using System.ByteBuffer;
using gameplatform;
using UnityEngine.SceneManagement;
using evt;
using Login;
using Player;
using info;
using Zjh;
using Dialog;
using Shop;
namespace tools{
public class CoderFactory  {

	public static void DecodeByte(byte[] source){

		MyConverter conver = new MyConverter (source);

		int head = conver.toInt32 ();

		Debug.Log ("消息头为:" + head);

		switch ( head ) {
			case 105201:
				ResShopItemsMsg shopmsg = new ResShopItemsMsg ();
				shopmsg.read (conver);
				ZJHShop.msg = shopmsg;
				break;
			case 100250:
				ResHeartbeatMsg heartmsg = new ResHeartbeatMsg ();
				heartmsg.read (conver);
				Heart.SendNum = 0;
				break;
			case 101201:
				
				ResPlayerInfoMsg msg = new ResPlayerInfoMsg ();
				msg.read (conver);
				UserInfo.UserData =	msg.playerInfo;
				break;
			case 101205:

				ResDiamondChangeMsg DiamondChange = new ResDiamondChangeMsg ();
				DiamondChange.read (conver);
				if(UserInfo.UserData != null){
					UserInfo.UserData.diamond =	DiamondChange.diamond;
				}

				break;
			case 102201:
				Chat.ResTextChatMsg chat = new Chat.ResTextChatMsg ();
				chat.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.FACE_ZJH_SHOW, chat);
				break;
			case 103202:
				Notice.ResMarqueeNoticeMsg notice = new Notice.ResMarqueeNoticeMsg ();
				notice.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.SHOW_TOP_NOTICE, notice.content);
				break;
			case 102202:
				Chat.ResVoiceChatMsg voice = new Chat.ResVoiceChatMsg ();
				voice.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.VOICE_ZJH_SHOW, voice);
				break;
			case 100203:

				ResGameStartMsg gamestart = new ResGameStartMsg ();
				gamestart.read (conver);
				MainPage.gamestart = gamestart;

				Heart.SendNum = 0;

				Debug.Log ("开始游戏------------------------------------房间号:"+ info.UserInfo.UserData.roomId);
				ServerEventDispather.getInstance ().Add (ServerEvent.LOGIN_RESULT, null);

				if (!GSocket.reConnection) {
					//ServerEventDispather.getInstance ().Add (ServerEvent.LOGIN_RESULT, null);
				} else {
					
					GSocket.reConnection = false;
					if ( info.UserInfo.UserData.roomId != 0 ) {
						GSocket.Send (ByteFactory.makeByte (621102,info.UserInfo.UserData.roomId.ToString()));
					}
				}
				break;
			case 621207:
				thirteen.ResEnterRoomMsg enterRoom = new thirteen.ResEnterRoomMsg ();
				enterRoom.read (conver);

				UserInfo.room = enterRoom;
				ServerEventDispather.getInstance ().Add (ServerEvent.CREATE_ROOM_RESULT,null);
				break;
			case 621209:
				thirteen.ResOtherEnterRoomMsg seats = new thirteen.ResOtherEnterRoomMsg ();
				seats.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.OTHER_JOIN_ROOM,seats);
				break;
			case 621215:
				ResReadyMsg readyMsg = new ResReadyMsg ();
				readyMsg.playerId = conver.toInt64 ();
				for( int i = 0 ; i < UserInfo.room.seats.Count ; i++ ){
					if( readyMsg.playerId == UserInfo.room.seats[i].playerId ){
						UserInfo.room.seats [i].ready = 1;

					}
				}

				ServerEventDispather.getInstance ().Add (ServerEvent.CHANGE_ZJH_PLAYER_STATE,readyMsg);
				break;
				//发牌
			case 621217:
				thirteen.ResReadyOverMsg ResReadyOverMsg = new thirteen.ResReadyOverMsg ();
				ResReadyOverMsg.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.CHANGE_ZJH_DEALER,ResReadyOverMsg);
				break;
			case 621219:
				ResBetMsg betmsg = new ResBetMsg ();
				betmsg.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.NEXT_ZJH_DEAL,betmsg);
				break;
			case 621221:
				ResSeeCardMsg seeCard = new ResSeeCardMsg ();
				seeCard.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.SEE_ZJH_CARD,seeCard);
				break;
			case 621223:
				ResSawCardMsg sawCard = new ResSawCardMsg ();
				sawCard.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.SAW_ZJH_CARD,sawCard);
				break;
			case 621225:
				ResDiscardMsg discardmsg = new ResDiscardMsg ();
				discardmsg.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.RENOUNCE_ZJH_CARD,discardmsg);
				break;
			case 621227:
				thirteen.ResVersusMsg versusmsg = new thirteen.ResVersusMsg ();
				versusmsg.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.COMPARE_ZJH_CARD,versusmsg);
				break;
			case 621231:
				thirteen.ResRoundOverMsg overmsg = new thirteen.ResRoundOverMsg ();
				overmsg.read (conver);
				ZjhSettlement.Settlement = true;
				ServerEventDispather.getInstance ().Add (ServerEvent.ROUNDOVER_ZJH_CARD,overmsg);
				break;
			case 621205:
				thirteen.ResDisbandRoomAbleMsg DisbandMsg = new thirteen.ResDisbandRoomAbleMsg ();
				DisbandMsg.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.CLOSEROOM_ZJH_ALERT,DisbandMsg);
				break;
			case 621201:
				thirteen.ResConfirmDisbandRoomMsg confirmMsg = new thirteen.ResConfirmDisbandRoomMsg ();
				confirmMsg.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.CLOSEROOM_ZJH_CONFRIM,confirmMsg);
				break;
			case 621211:
				ResLeaveRoomMsg leaveMsg = new ResLeaveRoomMsg ();
				leaveMsg.read (conver);
				ServerEventDispather.getInstance ().Add (ServerEvent.LEAVEROOM_ZJH_RESULT,leaveMsg.playerId);
				break;

			case 621203:
				thirteen.ResDisbandRoomOverMsg DisbandRoomOverMsg = new thirteen.ResDisbandRoomOverMsg ();
				DisbandRoomOverMsg.read (conver);

				//if(DisbandRoomOverMsg.success != 0){
					ServerEventDispather.getInstance ().Add (ServerEvent.CLOSE_ZJH_DISBAND,UserInfo.UserData.id);
				//}

				break;
			case 621213:

				thirteen.ResCeditChangeMsg ceditMsg = new thirteen.ResCeditChangeMsg ();
				ceditMsg.read (conver);

				for( int s = 0 ; s < UserInfo.room.seats.Count ; s++ ){
					if (ceditMsg.playerId == UserInfo.room.seats [s].playerId) {
						UserInfo.room.seats [s].cedit = ceditMsg.cedit;
					}
				}

				ServerEventDispather.getInstance ().Add (ServerEvent.UPDATE_ZJH_CEDIT,ceditMsg);

				break;
			case 621233:

				ResGameOverMsg gameover = new ResGameOverMsg ();
				gameover.read (conver);

				List<RecordInfo> list = new List<RecordInfo> ();
				list.Add (gameover.record);
				for (int i = 0; i < UserInfo.records.Count; i++) {
					list.Add (UserInfo.records [i]);
				}
				UserInfo.records = list;

				if (ZjhSettlement.Settlement) {
					ZjhSettlement.GameAllOver = true;
					ServerEventDispather.getInstance ().Add (ServerEvent.UPDATE_ZJH_SUMUP, gameover);
				} else {
					ServerEventDispather.getInstance ().Add (ServerEvent.UPDATE_ZJH_SUMUP, gameover);
					ServerEventDispather.getInstance ().Add (ServerEvent.ALLOVER_ZJH_VIEW, gameover);
				}

				break;
			case 621237:

				ResRecordsMsg record = new ResRecordsMsg ();
				record.read (conver);
				UserInfo.records = record.records;
				UserInfo.records.Reverse ();
				break;

			case 400201:
				ResErrorMsg error = new ResErrorMsg ();
				error.read (conver);
				if (Application.platform == RuntimePlatform.IPhonePlayer) {
					string msgs = null;
					AlertTxt tx = new AlertTxt ();
					msgs = tx.dic[error.msgId.ToString()]; 
					msgs = StringTools.BuildStr (error.args, msgs);
					ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, msgs);
				} else {
					string errorStr = StringTools.BuildStr (error.args, fil.ConfigManager.getInstance ().readDictionaryForId ("Alert", "id", error.msgId.ToString ()) ["msg"]);
					ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, errorStr);
				}

				break;

			case 400202:
				ResWarnMsg warn = new ResWarnMsg ();
				warn.read (conver);
				if (Application.platform == RuntimePlatform.IPhonePlayer) {
					string msgs = null;
					AlertTxt tx = new AlertTxt ();
					msgs = tx.dic[warn.msgId.ToString()]; 
					msgs = StringTools.BuildStr (warn.args, msgs);
					ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, msgs);
				} else {
					string warnStr = StringTools.BuildStr(warn.args,fil.ConfigManager.getInstance ().readDictionaryForId ("Alert", "id",warn.msgId.ToString()) ["msg"]);
					ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, warnStr);
				}

				break;

			case 400203:

				if (Application.platform == RuntimePlatform.IPhonePlayer) {
					return;
				}

				ResTipMsg tip = new ResTipMsg ();
				tip.read (conver);

				string str = fil.ConfigManager.getInstance ().readDictionaryForId ("Alert", "id", tip.msgId.ToString ()) ["msg"];
				string tipStr;

				if(str == null|| str == ""){
					tipStr = tip.Desc;
				}else{
					tipStr = StringTools.BuildStr(tip.args,str);
				}


				ServerEventDispather.getInstance ().Add (ServerEvent.SHOW_TOP_NOTICE, tipStr);
				break;

			case 400204:
				if (Application.platform == RuntimePlatform.IPhonePlayer) {
					return;
				}
				ResShowMsg show = new ResShowMsg ();
				show.read (conver);
				string showStr = StringTools.BuildStr(show.args,fil.ConfigManager.getInstance ().readDictionaryForId ("Alert", "id",show.msgId.ToString()) ["msg"]);
				ServerEventDispather.getInstance ().Add (ServerEvent.SHOW_DOWN_NOTICE, showStr);
				break;

			case 621235:
				ResRestoreMsg restore = new ResRestoreMsg ();
				restore.read (conver);
				ZjhView.restore = restore;
				ServerEventDispather.getInstance ().Add (ServerEvent.RECOVERY_ZJH_BOARD, restore);
				break;
			case 621229:
				ResOfflineMsg offline = new ResOfflineMsg ();
				offline.playerId = conver.toInt64 ();
				ServerEventDispather.getInstance ().Add (ServerEvent.OFFINE_ZJH_PLAYER, offline);
				break;
			default:break;

			}
		}
	}
}
